package com.zfcoding.redismybatisplus.middlewares;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@ControllerAdvice
public class MyExceptionHandler
{
    private static Logger logger = LoggerFactory.getLogger(MyExceptionHandler.class);

    @ExceptionHandler(value = Exception.class)
    public void defaultExceptionHandler(HttpServletRequest req, HttpSession session,Exception e)
    {
        String errorSource = session.getAttribute("errorSource").toString();

        if (e instanceof java.lang.NullPointerException){
            logger.info("空指针异常 --- 出错的方法是 --> "+errorSource);
        }else if (e instanceof java.lang.ArithmeticException){
            logger.info("使用0作为分母 --- 出错的方法是 -->"+ errorSource);
        }

        e.printStackTrace();
        System.out.println(" --- 全局异常捕捉中 --- ");
    }

}
